package com.giteelou.antlr.ps.man.type;

/**
 * 基本类型
 */
public enum PrimitiveType implements Type {
    BOOLEAN("boolean"),
    CHAR("char"),
    BYTE("byte"),
    SHORT("short"),
    INT("int"),
    LONG("long"),
    FLOAT("float"),
    DOUBLE("double");

    private final String name;

    PrimitiveType(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return getName();
    }

    public static boolean isTypeMatched(Type declaredType, Type matchedType) {
        if (declaredType == matchedType) {
            return true;
        }
        if (LONG == declaredType) {
            return matchedType == INT;
        }
        if (DOUBLE == declaredType) {
            return matchedType == FLOAT
                    || matchedType == LONG
                    || matchedType == INT
                    || matchedType == CHAR;
        }
        if (FLOAT == declaredType) {
            return matchedType == LONG
                    || matchedType == INT
                    || matchedType == CHAR;
        }
        if (BYTE == declaredType
                || SHORT == declaredType) {
            return matchedType == INT
                    || matchedType == CHAR;
        }
        if (CHAR == declaredType) {
            return matchedType == INT;
        }
        return INT == declaredType
                && CHAR == matchedType;
    }
}
